package com.qian.controller;

import com.qian.pojo.Employees;
import com.qian.service.EmployeeService;
import com.qian.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emp")
@CrossOrigin(allowCredentials = "true")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @Resource
    private UserService userService;

    //查询本租户内的所有员工
    @RequestMapping("/searchAll")
    public List<Employees> searchAll(Model model) {
        List<Employees> employeesList = employeeService.searchAllEmployee();
        model.addAttribute("employeesList", employeesList);
        return employeesList;
    }

    //登录系统
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map map, HttpSession session) {
        String employeeAccount = (String) map.get("employeeAccount");
        String employeePassword = (String) map.get("employeePassword");
        Map<String, Object> res = new HashMap<String, Object>();
        List<Employees> list = employeeService.employeeLogin(employeeAccount);
        if (list.size() == 0) {
            res.put("msg", 501);
        } else {
            if (!list.get(0).getEmployeePassword().equals(employeePassword)) {
                res.put("msg", 502);
            } else {
                Employees old = list.get(0);
                session.setAttribute("emp", old);
                //将租户标志加入到session中，方便之后的相关操作进行过滤
                session.setAttribute("tenantId", old.getTenantId());
                session.setAttribute("employeeId", old.getEmployeeId());
                session.setAttribute("employeeCompany", old.getEmployeeCompany());
                res.put("msg", 200);
                res.put("emp",old);
            }
        }
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //返回session信息
    @RequestMapping("/getSession")
    public ResponseEntity<Map<String, Object>> getSession(HttpSession session) {
        Map res = new HashMap();
        res.put("emp", session.getAttribute("emp"));
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //注销
    @RequestMapping("/logout")
    public void logout(HttpSession session) {
        session.removeAttribute("emp");
    }

    //获取信息
    @RequestMapping(value = "/getInfo",method = RequestMethod.GET)
    public ResponseEntity getInfo(HttpSession session) {
        Employees emp = userService.searchUserById((Long) session.getAttribute("employeeId"));
        HashMap res = new HashMap();
        res.put("emp",emp);
        return new ResponseEntity(res,HttpStatus.OK);
    }

    //修改密码
    @RequestMapping(value = "/updatePsd", method = RequestMethod.POST)
    public ResponseEntity updatePsd(@RequestBody Map map, HttpSession session) {
        String password = (String) map.get("employeePassword");
        Long employeeId = (Long) session.getAttribute("employeeId");
        userService.updatePsd(employeeId, password);
        HashMap res = new HashMap();
        res.put("msg", "密码修改成功！");
        return new ResponseEntity(res, HttpStatus.OK);
    }


    //查询当前所有租户
    @RequestMapping(value = "/allTenants",method = RequestMethod.GET)
    public ResponseEntity allTenants(){
        HashMap res = new HashMap();
        res.put("allTenants",employeeService.allTenants());
        return new ResponseEntity(res,HttpStatus.OK);
    }
}
